var app = getApp();
Page({
	data: {
		orderlist: null,
		dealer: null,
		over: 0,
		type: 0,
		opens: 0,
		userCouponList: [], //优惠券列表
		couponindex: -1, //可用优惠券索引
		couponpice: 0, //优惠金额
		couponafter: 0 //优惠后
	},
	onLoad(options) {
		var that = this;
		this.data.type = options.type
		this.setData({
			dealer: wx.getStorageSync('dealer'),
			orderlist: wx.getStorageSync('orderlist')
		});
		//加载优惠券数据
		app.globalAjax(
			'/api/user/coupon',
			"get", {},
			function(e) {
				if (e.code == 0) {
					//判断优惠券是否可用
					var couponindex = -1;
					for (var i in e.userCouponList) {
						var $t = e.userCouponList[i],
					        full = $t.moneyFull;
						if (Number(that.data.orderlist.total) >= Number(full)) {
							if (couponindex == -1) {
								couponindex = i; //赋值默然优惠券
							}
							$t.types = 1; //可用
						} else {
							$t.types = 0; //不可用
						}
					};
					//end 
					that.setData({
						couponindex: couponindex,
						userCouponList: e.userCouponList
					});
					that.setpice() //设置金额

				} else {
					wx.showToast({
						title: e.msg,
						icon: "none"
					});
				}
			})
		//end

	},
	setopens: function() { //弹出优惠
		var $th = this;
		var o = $th.data.opens ? 0 : 1;
		$th.setData({
			opens: o
		});
	},
	selectcoupon: function(e) { //选择优惠
		var $th = this,
			cur = e.currentTarget.dataset;
		if (cur.types == 0) return false;
		$th.setData({
			couponindex: cur.index
		});
		$th.setopens()
		$th.setpice() //设置金额
	},
	setpice: function(e) { //设置金额
		var $th = this,
			opirce = $th.data.orderlist.total, //原始总价
			cpirce = 0, //优惠金额
			apirce = 0 //优惠后金额

		if ($th.data.couponindex != -1) { //有优惠券可用
			cpirce = $th.data.userCouponList[$th.data.couponindex].moneyReduce;
		}
		apirce = Number(opirce) - Number(cpirce);
		$th.setData({
			couponpice: parseFloat(cpirce).toFixed(2), //优惠金额
			couponafter: parseFloat(apirce).toFixed(2) //优惠后
		});
	},
	sub: function() { //提交订单 并付款
	    var userInfo = wx.getStorageSync('userInfo')
		if(!userInfo.mobile || !userInfo.realName){
			wx.showModal({
				title:'温馨提示',
				content:'请先填写个人信息以便发货',
				showCancel:false,
				success: function(res){
					wx.navigateTo({
						url: '/pages/member/personal/personal'
					})
				}
			})
			return
		}
		var $th = this;
		var that = this.data;
		var couponId = '';
		if ($th.data.couponindex != -1) { //有优惠券可用
			couponId = $th.data.userCouponList[$th.data.couponindex].couponId;
		}
		var datas = {
			orderGoodsList: that.orderlist.orderGoodsList,
			remark: "",
			storeId: that.dealer.id,
			totalAmount: that.couponafter,
			couponId: couponId
		};
		app.globalAjax(
			'/api/order/create',
			"post",
			datas,
			function(e) {
				if (e.code == 0) {
					//如果是购物车下单 移除购物车
					if ($th.data.type == "cart") {
						var cart = wx.getStorageSync('cart');
						for (var i in datas.orderGoodsList) {
							var t = datas.orderGoodsList[i]
							var sets = cart[t.categoryId + "_" + t.goodsId + "_" + t.specsId]
							if (sets) {
								delete cart[t.categoryId + "_" + t.goodsId + "_" + t.specsId];
							}
						}
						wx.setStorage({
							key: "cart",
							data: cart //需要存储的数据
						})
					}
					//end

					//跳转付款
					app.globalAjax(
						'/api/unifiedOrder',
						"post", {
							orderNumber: e.orderNumber,
							totalAmount: 0.1
						},
						function(e2) {
							if (e2.code == 0) {
								if(!e2.data){
									wx.showToast({
										title: '支付成功'
									})
									setTimeout(function() {
										wx.redirectTo({
											url: '/pages/order/order/order?type=0'
										})
									}, 500)
									return
								}
								app.pays(e2.data, function(ee) {
									$th.setData({
										over: 1
									});
								}, function(ee) {
									setTimeout(function() {
										wx.redirectTo({
											url: '/pages/order/order/order?type=0'
										})
									}, 500)
								})
							} else {
								wx.showToast({
									title: e2.msg,
									icon: "none"
								});
							}
						}, {
							token: wx.getStorageSync("token")
						})

				} else {
					wx.showToast({
						title: e.msg,
						icon: "none"
					});
				}
			}, {
				token: wx.getStorageSync("token")
			})

	}
})
